package com.sc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.bean.T_card;
import com.sc.bean.T_waiting;
import com.sc.bean.User;
import com.sc.serviceimpl.ApproverImpl;
import com.sc.serviceimpl.CardServiceImpl;
import com.sc.serviceimpl.WaitingServiceImpl;


@Controller
public class CardController {

	@Autowired
	private CardServiceImpl cardServiceImpl;
	
	@Autowired
	private ApproverImpl approverImpl;
	
	@Autowired
	private WaitingServiceImpl waiServiceImpl;
	
	@RequestMapping("addCard")
	public String addCard(HttpServletRequest request,Model model){
		String fan="";
		T_card card=new T_card();
	
		card.setC_approver(request.getParameter("approver"));
		card.setC_bei(request.getParameter("bei"));
		card.setC_day(request.getParameter("day"));
		System.out.println(request.getParameter("day"));
		card.setC_dept(request.getParameter("dept"));
		card.setC_uname(request.getParameter("uname"));
		int number= Integer.parseInt(request.getParameter("number"));	
		card.setC_number(number);
		card.setC_phone(request.getParameter("phone"));
		card.setC_post(request.getParameter("post"));
		card.setC_telephone(request.getParameter("telephone"));
		int i = cardServiceImpl.add(card);		
		
		//申请失败时返回前端
		List<User> userList=approverImpl.find();		
		model.addAttribute("userList",userList);
		model.addAttribute("str", 222);
		String result="";
		
		if(i!=1){
			result="申请失败";
			fan="addCard";
			System.out.println( "申请失败");
		}else{						
			System.out.println( "申请成功");
			T_waiting wai=new T_waiting();
			wai.setWt_pname(request.getParameter("uname"));//申请人名字
			wai.setWt_tname(request.getParameter("uname")+"名片申请");//
			wai.setWt_status("未审核");
			wai.setWt_starttime(request.getParameter("cday"));
			wai.setWt_table("t_card");
			int a = waiServiceImpl.add(wai);

			if(a!=1){	
				System.out.println("添加审核失败");
			}else{
				fan="waiting";
				System.out.println("添加审核成功");
			}
		}
		model.addAttribute("result",result);
		return fan;
	}

	
	
	@RequestMapping("queryWai")
	public String queryWai(Model model,int curr){
		
		System.out.println("第几页curr="+curr);
		PageHelper.startPage(curr, 5);
		List<T_waiting> waiList=waiServiceImpl.find();
		PageInfo<T_waiting> waiPage=new PageInfo<T_waiting>(waiList);
		
		int page =waiServiceImpl.findCount();
		waiPage.setTotal(page);
		
		System.out.println("查询待处理任务");
		System.out.println(waiList);
		System.out.println("pages==="+waiPage.getPages());
		System.out.println("上一页==="+waiPage.getPrePage());
		System.out.println("下一页==="+waiPage.getNextPage());
		System.out.println("第一页==="+waiPage.getFirstPage());
		System.out.println("最后一页==="+waiPage.getLastPage());
		System.out.println("这是啥="+waiPage.getNavigateLastPage());
		model.addAttribute("waiList", waiList);
		model.addAttribute("str", 333);
		model.addAttribute("waitCurr", curr);
		model.addAttribute("waitPage", waiPage);
		return "waiting";
	}
	
	@RequestMapping("queryCard")
	public String queryCard(Model model,int num){
	
		List<T_card> cardList=cardServiceImpl.find();

		System.out.println("查询待处理任务");
		System.out.println(cardList);
		model.addAttribute("cardList", cardList);
		model.addAttribute("str", 333);
		return "waiting";
	}
	
}
